﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WaterBill.BUS;

namespace WaterBill.GUI
{
    public partial class frmStatistics : DevExpress.XtraEditors.XtraForm
    {
        public frmStatistics()
        {
            InitializeComponent();
        }
        BUS_Charges busCharge = new BUS_Charges();
        BUS_Bills busBill = new BUS_Bills();
        private void rdgStatistic_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rdgStatistic.Properties.Items[rdgStatistic.SelectedIndex].ToString() == "Theo năm")
            {
                panelMonth.Visible = false;
                panelYear.Visible = true;
            }
            else {
                panelMonth.Visible = true;
                panelYear.Visible = false;
            }
        }

        private void frmStatistics_Load(object sender, EventArgs e)
        {
            //load cbbYear
            cbbYear.DataSource = busCharge.BUS_Charge_GetAllYear();
            cbbYear.DisplayMember = "Year";
            cbbYear.ValueMember = "Year";
            //load cbbFromMonth
            cbbFromMonth.DataSource = busCharge.BUS_Charge_GetAllMonth();
            cbbFromMonth.DisplayMember = "Month";
            cbbFromMonth.ValueMember = "Month";
            //load cbbToMonth
            cbbToMonth.DataSource = busCharge.BUS_Charge_GetAllMonth();
            cbbToMonth.DisplayMember = "Month";
            cbbToMonth.ValueMember = "Month";
            //load cbbFromYear
            cbbFromYear.DataSource = busCharge.BUS_Charge_GetAllYear();
            cbbFromYear.DisplayMember = "Year";
            cbbFromYear.ValueMember = "Year";
            //load cbbToYear
            cbbToYear.DataSource = busCharge.BUS_Charge_GetAllYear();
            cbbToYear.DisplayMember = "Year";
            cbbToYear.ValueMember = "Year";
            //Load Tong danh thu
            //lbRevenue.Text = string.Format("{0:0,0} VNĐ", busBill.BUS_Bills_GetTotalPrice().Rows[0]["Total"]);
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (rdgStatistic.Properties.Items[rdgStatistic.SelectedIndex].ToString() == "Theo năm")
            {
                if (Convert.ToInt32(cbbFromYear.SelectedValue) <= Convert.ToInt32(cbbToYear.SelectedValue))
                {
                    DataTable dt = busBill.BUS_Bills_GetYearStats(Convert.ToInt32(cbbFromYear.SelectedValue), Convert.ToInt32(cbbToYear.SelectedValue));
                    object total = dt.Compute("sum(Sumrevenue)", "");
                    gcStats.DataSource = dt;
                    gvstats.Columns[0].Visible = false;
                    lbRevenue.Text = string.Format("{0:0,0} VNĐ", total);
                }
                else
                {
                    XtraMessageBox.Show("Lỗi, chọn năm không hợp lệ.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else {
                if (Convert.ToInt32(cbbFromMonth.SelectedValue) <= Convert.ToInt32(cbbToMonth.SelectedValue))
                {
                    DataTable dt = busBill.BUS_Bills_GetMonthStats(Convert.ToInt32(cbbFromMonth.SelectedValue), Convert.ToInt32(cbbToMonth.SelectedValue), Convert.ToInt32(cbbYear.SelectedValue));
                    object total = dt.Compute("sum(Sumrevenue)", "");
                    gcStats.DataSource = dt;
                    gvstats.Columns[0].Visible = true;
                    lbRevenue.Text = string.Format("{0:0,0} VNĐ", total);
                }
                else
                {
                    XtraMessageBox.Show("Lỗi, chọn tháng không hợp lệ.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}